const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require('../../libs/regenerator');
const co = require("../../libs/co");
const wxPassport = require("./passport.wx");
const http = require('../../libs/httpRequest');
const appConfig = require('../../appConfig');

var Passport = function () {

    var _wxLogin = function *(isRequired) {
        var isOnline = yield wxPassport.checkSession();
        if (!isOnline || isRequired){
            yield wxPassport.isAuthorized();
            var authorizeCode = yield wxPassport.login();
            var userInfo = yield wxPassport.getUserInfo();
            userInfo.authorizeCode = authorizeCode;
            return userInfo;
        }else{
            console.log('Online');
        }
    }
    var _wxOpenAuthorize = function *() {
        var isAuthorized = yield wxPassport.openAuthorize();
        return isAuthorized;
    }
    var _authorize = function *(profile) {
        console.log("passport: authorize");
        var ret = yield http.request({
            url: appConfig.api.baseUrl + "/authorize/login",
            data: profile,
            method: 'POST',
            success: (res) => {
                console.log(res.data);
            },
            fail: (err) => {
                console.log(err);
            }
        });
        return ret;
    }
    return {
        wxLogin: _wxLogin,
        wxOpenAuthorize: _wxOpenAuthorize,
        authorize: _authorize
    }
}();

module.exports = Passport;
